package com.itcam.sort.selectionsort;

import java.util.Arrays;

/**
 * @author : Cammy.Wu
 * Description : 选择排序
 */

public class SelectionSort {

    public static void main(String[] args) {
        int a[] = {9, 5, 7, 12, 1, 3};
        System.out.println(Arrays.toString(a));
        selection(a);
        System.out.println(Arrays.toString(a));

    }

    private static void selection(int[] a) {
        // 1.选择轮数，a.length - 1
        // 2.交换的索引位置(right) 初始值 a.length - 1 每次递减
        for (int right = a.length - 1; right > 0; right--) {
            int max = right;
            for (int i = 0; i < right; i++) {
                if (a[i] > a[max]) {
                    max = i;
                }
            }
            swap(a, max, right);
        }
    }

    private static void swap(int[] a, int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}
